package com.helloandroid.tools

import java.text.SimpleDateFormat
import java.time.LocalDate
import java.util.*

object MyTimeUtils{

    /**
     *
     * @param time 时间戳
     * @return 如: 2020-01-01 22:23:12
     */
    fun fullString(time: Long):String = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date(time))

    fun customString(pattern: String, time: Long):String = SimpleDateFormat(pattern).format(Date(time))

    /**
     *
     * @param time 时间戳
     * @return 如: 2020-01-01
     */
    fun dayTimeString(time: Long):String = SimpleDateFormat("yyyy-MM-dd").format(Date(time))

    /**
     *
     * @param time 时间戳
     * @return 如: 23:36:34
     */
    fun timeString(time: Long):String = SimpleDateFormat("HH:mm:ss").format(Date(time))

    /**
     *
     * @param time 时间戳
     * @return 如: 36:34
     */
    fun minutesAndSeconds(time: Long):String = SimpleDateFormat("mm:ss").format(Date(time))

    /**
     * 格式化显示 mm:ss
     * @param betweenMs  如:  713000ms
     * @return  如: 11:53
     */
    fun format_mmss(betweenMs: Long):String{
        var bet = betweenMs;
        if(bet > 3600 * 1000){
            val hour = (bet / (3600 * 1000)).toInt()
            bet -= (hour * 3600 * 1000)
        }
        val minutes = (bet/(60 * 1000)).toInt()
        val seconds = ((bet - minutes * (60 * 1000)) / 1000).toInt()
        return with(StringBuilder()) {
            if (minutes < 10) {
                append("0")
            }
            append(minutes)
            append(":")
            if (seconds < 10) {
                append("0")
            }
            append(seconds)
            toString()
        }
    }

    /**
     * 格式化显示 hh:mm
     * @param betweenMs  如:  11525000ms
     * @return  如: 03:12
     */
    fun format_hhmm(betweenMs: Long):String{
        var bet = betweenMs;
        val hour = (bet / (3600 * 1000)).toInt()
        bet -= (hour * 3600 * 1000)
        val minutes = (bet / (60 * 1000)).toInt()
        return with(StringBuilder()){
            if(hour < 10){
                append("0")
            }
            append(hour)
            append(":")
            if(minutes < 10){
                append("0")
            }
            append(minutes)
            toString()
        }
    }

    /**
     *  格式化显示 HH:mm:ss
     * @param betweenMs
     * @return 如:01:11:53
     */
    fun format_HHmmss(betweenMs: Long):String{
        var bet = betweenMs;
        val hour = (bet / (3600 * 1000)).toInt()
        bet -= (hour * 3600 * 1000)
        val minutes = (bet / (60 * 1000)).toInt()
        bet -= (minutes * 60 * 1000)
        val seconds = (bet / 1000).toInt()
        return with(StringBuilder()){
            if(hour < 10){
                append("0")
            }
            append(hour)
            append(":")
            if(minutes < 10){
                append("0")
            }
            append(minutes)
            append(":")
            if(seconds < 10){
                append("0")
            }
            append(seconds)
            toString()
        }
    }

    /**
     * 根据 stamp 时间所在的日期, 返回 当天 hour:minutes:seconds 时候的 stamp
     * @param stamp
     * @param hour
     * @param minutes
     * @param seconds
     * @return
     */
    fun getSpecifiedStampByStamp(stamp: Long, hour: Int, minutes: Int = 0, seconds: Int = 0):Long =
            getStartDayMs(stamp) + hour * 3600 * 1000 + minutes * 60 * 1000 + seconds * 1000

    /**
     * 返回 stamp 所在天起点的 时间错
     * @param stamp
     * @return
     */
    fun getStartDayMs(stamp: Long):Long{
        return with(Calendar.getInstance()){
            timeInMillis = stamp
            val h = get(Calendar.HOUR_OF_DAY)
            val m = get(Calendar.MINUTE)
            val s = get(Calendar.SECOND)
            val ms = get(Calendar.MILLISECOND)
            stamp - (h * 3600 + m * 60 + s) * 1000 - ms
        }
    }

    /**
     * 根据系统时间,获取 从今天 00:00 开始的时间戳
     * 相当于  LocalTime
     */
    fun getTodayOffsetMs(stamp: Long):Long{
        return with(Calendar.getInstance()){
            timeInMillis = stamp
            val h = get(Calendar.HOUR_OF_DAY)
            val m = get(Calendar.MINUTE)
            val s = get(Calendar.SECOND)
            val ms = get(Calendar.MILLISECOND)
            ((h * 3600 + m * 60 + s) * 1000 + ms).toLong()
        }
    }

    fun getOffsetTimeMs(hour: Int, minutes: Int): Long {
        return (hour * 3600 * 1000 + minutes * 60 * 1000).toLong()
    }


    // date 形如 "2015-12-7T16:00:00.000Z"
    // 转换成 时间戳
    fun transformTimeByZone(dateStr: String?):Long{
        //     //UTC就是世界标准时间，与北京时间相差八个时区。所以只要将UTC时间转化成一定格式的时间，再在此基础上加上8个小时就得到北京时间了。
        if (dateStr.toString() != "null"){
            val date = dateStr!!.replace("Z", " UTC") //注意是空格+UTC
            val format = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z") //注意格式化的表达式
            return format.parse(date).time - 8 * 3600 * 1000L
        }else{
            return 0L
        }
    }

    // 将当前时间转换成  20210528 这样的int整数
    fun timeToInt(timeMs:Long):Int{
        val str = SimpleDateFormat("yyyyMMdd").format(Date(timeMs))
        return str.toInt()
    }

    // 将 "2021-4-27" 的时间格式 转换成  20210427 这样的 int 整数
    fun simpleDayTimeStringToInt(simpleDayTime:String):Int{
        val arr = simpleDayTime.trimIndent().split("-")
        if (arr.size != 3){
            throw RuntimeException("simpleDayTime:${simpleDayTime} 格式错误!")
        }
        val year = arr[0].toInt()
        val month = arr[1].toInt()
        val day = arr[2].toInt()

        val sb = StringBuilder()
        sb.append(year)
        if (month <  10){
            sb.append("0")
        }
        sb.append(month)

        if (day < 10){
            sb.append("0")
        }
        sb.append(day)
        return sb.toString().toInt()
    }


    // 返回  20210427 这样的 int 整数  前/后 dayCount 个
    fun moveDayInt(dayInt:Int,dayCount:Int):Int{
        val str = dayInt.toString()
        val year = str.substring(0,4).toInt()
        val month = str.substring(4,6).toInt()
        val day = str.substring(6).toInt()

        val calendar = Calendar.getInstance()
        calendar.set(Calendar.YEAR,year)
        calendar.set(Calendar.MONTH,month  - 1)
        calendar.set(Calendar.DAY_OF_MONTH,day)

        //
        calendar.add(Calendar.DAY_OF_MONTH,dayCount)

        val newYear = calendar.get(Calendar.YEAR)
        val newMonth = calendar.get(Calendar.MONTH) + 1
        val newDay = calendar.get(Calendar.DAY_OF_MONTH)
        val sb = StringBuilder()
        sb.append(newYear)
        if (newMonth < 10){
            sb.append("0")
        }
        sb.append(newMonth)

        if (newDay < 10){
            sb.append("0")
        }
        sb.append(newDay)
        return sb.toString().toInt()
    }


    // 返回  20210427 这样的 int 整数 的下一个
    fun nextDayInt(dayInt:Int):Int{
        return moveDayInt(dayInt,1)
    }

    // 返回  20210427 这样的 int 整数 的前一个
    fun preDayInt(dayInt:Int):Int{
       return moveDayInt(dayInt,-1)
    }
}
